<?php
if (!defined('INDEX')) exit;
require("common/functions_jcas_modules.php");
$cfg['debug_enable'] = true;
$cfg["online_per_page"] = 30;
if($_GET['p']=='show') $_GET['p']='online';
if(!@$_GET['year']) $_GET['year'] = show_date('Y',$time);
$current_month = show_date('n',$time);

?>
<table width="100%"  border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td class="datatd" bgcolor="#FFFFFF"><?php echo $lang['stats_desc']?></td>
  </tr>
</table>

<?php
// Add page hit
echo stats_add_page_hit();
// Delete stats
echo common_delete($tbl['page_hit'],'id');

/*
stats_reset_counter();
$visitor = $DB->fetch_array($DB->query('SELECT SUM(visitors) AS total_visitors FROM '.$tbl["visitor"].' WHERE id!=0'));
$start_count = $DB->fetch_array($DB->query('SELECT * FROM '.$tbl["visitor"].' ORDER BY id ASC LIMIT 0,1'));
$avg_visitor = floor($visitor['total_visitors']/ceil(($time - $start_count['updated_time'])/(24*60*60)));
*/
?>
<br />
<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="110" class="<?php echo ((@$_GET['p']=='online')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=online"><?php echo $lang['online_users']?></a></td>
        <td width="5">&nbsp;</td>
        <td width="110" class="<?php echo ((@$_GET['p']=='visitor')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=visitor"><?php echo $lang['visitor_stats']?></a></td>
        <td width="5">&nbsp;</td>
		<!--
		<td width="150" class="<?php echo ((@$_GET['p']=='page_hit')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=page_hit"><?php echo $lang['page_hit_stats']?></a></td>
        <td width="5">&nbsp;</td>
		 
        <td width="110" class="<?php echo ((@$_GET['p']=='browser')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=browser"><?php echo $lang['browser_stats']?></a></td>
        <td width="5">&nbsp;</td>
        <td width="110" class="<?php echo ((@$_GET['p']=='platform')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=platform"><?php echo $lang['platform_stats']?></a></td>
        <td width="5">&nbsp;</td>
        <td width="110" class="<?php echo ((@$_GET['p']=='tracking')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=tracking"><?php echo $lang['link_stats']?></a></td>
		  -->
		  <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td class="tabtd" height="5"></td>
  </tr>
</table>
<?php
switch (@$_GET['p'])
{
	case "online":
	default:
		$sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM '.$tbl["session"];
		$q_online = $DB->query($sql.' ORDER BY '.((@$_GET['sort_by'])?$_GET['sort_by'].' ASC,':'').' updated_time DESC LIMIT '.($_GET["pg"]-1)*$cfg["online_per_page"].','.$cfg["online_per_page"]);
		$num_online = $DB->num_rows($q_online);
		
		$total_online = $DB->result($DB->query('SELECT FOUND_ROWS()'),0);
	?>
		<table width="100%"  border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
		<form name="frm_show" method="post" action="<?php echo $index_url.'&p=online&sort_by='.$_GET['sort_by']?>">
		  <tr>
			<td colspan="10" class="headtd"><?php echo $lang["online_user_title"]?>  ( <?php echo sprintf($lang['s_users'],$total_online)?> )</td>
			</tr>
		  <tr align="center">
			 <td width="30" class="subheadtd"><?php echo $lang['order']?></td>
			 <td width="50" class="subheadtd"><?php echo $lang['user_id']?></td>
				<td  class="subheadtd"><?php echo $lang['user_name']?></td>
				<td  class="subheadtd"><?php echo $lang['viewing_page']?></td>
				<td  class="subheadtd"><?php echo $lang['user_ip']?></td>
				<td class="subheadtd"><?php echo $lang['user_agent']?></td>
		  </tr>
		  <?php
		  if($num_online)
		  {
			  $online_no = 0;
			  for($x=0;$x<$num_online;$x++)
			  {
				$online_no += 1;
				$online = $DB->result_array($q_online,$x,'user_id,user_name,user_ip,user_agent,viewing_page,updated_time');
				
				if(eregi("google",$online['user_agent'])) $online['user_name'] = '<strong>Google Bot</strong>';
				elseif(eregi("yahoo",$online['user_agent'])) $online['user_name'] = '<strong>Yahoo Bot</strong>';
			  ?>
			  <tr  onMouseOver="rollon(this);" onMouseOut="rolloff(this);" height="20">
				 <td align="right" class="datatd" ><?php echo ($_GET["pg"]-1)*$cfg["online_per_page"] + $online_no?></td>
					<td align="right" class="datatd"><font color="red"><?php echo $online['user_id']?></font></td>
				 <td class="datatd"><font color="blue"><?php echo $online['user_name']?></font></td>
				 <td align="left" class="datatd"><?php echo $online['viewing_page']?></td>
				 <td align="right" class="datatd"><font class="info"><?php echo $online['user_ip']?></font></td>
				 <td align="left" class="datatd"><font class="info"><?php echo $online['user_agent']?></font></td>
			  </tr>
			<?php
			}
		}
		else
		{
		?>
			<tr>
				 <td colspan="20" align="center" class="datatd"><?php echo message($lang['no_requested_data'],'info')?></td>
			  </tr>
		<?php
		}
		  ?>
			  <tr>
			 <td colspan="10" align="right" >
				<table width="100%"  border="0" cellpadding="0" cellspacing="0">
				  <tr align="left">
					 <td width="10%" class="row1td">
					 <?php echo $lang['sort_by']?>
					 <select name="sort_field" onChange="menu_jump(this.options[this.selectedIndex].value);">
					 	<option value="<?php echo $index_url.'&p=online&'?>" <?php echo ((!@$_GET['sort_by'])?'selected':'')?>><?php echo $lang['default']?></option>
					 	<option value="<?php echo $index_url.'&p=online&sort_by=user_name'?>" <?php echo ((@$_GET['sort_by']=='user_name')?'selected':'')?>><?php echo $lang['user_name']?></option>
					 	<option value="<?php echo $index_url.'&p=online&sort_by=user_ip'?>" <?php echo ((@$_GET['sort_by']=='user_ip')?'selected':'')?>><?php echo $lang['user_ip']?></option>
						<option value="<?php echo $index_url.'&p=online&sort_by=viewing_page'?>" <?php echo ((@$_GET['sort_by']=='viewing_page')?'selected':'')?>><?php echo $lang['viewing_page']?></option>
					 </select>
					 </td>
				  </tr>
				</table>
			 </td>
		  </tr>
		  <tr>
			 <td colspan="10" align="center" class="datatd">
			 <?php echo show_pages($total_online,$cfg["online_per_page"],$index_url.'&p=online&sort_by='.$_GET['sort_by'])?>
			 </td>
		  </tr>
		 </form>
		</table>
	<?php
	break;
	
	case "visitor":
	?>
	
<table width="100%"  border="0" cellspacing="0" cellpadding="0" align="center">
		
				<tr>
			 <td bgcolor="#FFFFFF" class="datatd">
		<table width="100%"  border="0" cellspacing="0" cellpadding="0" align="center">
		
				<tr>
			 <td bgcolor="#FFFFFF">
		 <!--
		<table width="50%"  border="0" cellspacing="0" cellpadding="0" align="center">
		  <tr>
			 <td width="40%" class="row1td" ><b>Total visitors</b><br>From <?php echo show_date($cfg['date_format'],(($start_count['time'])?$start_count['time']:$time))?></td>
			 <td width="60%" class="row2td"><font style="font-size:18px "><b><?php echo intval($visitor['total_visitors'])?></b></font>&nbsp;&nbsp;&nbsp;[<a href="<?php echo $index_url?>&act=reset_counter" onClick="return confirm('Are you sure you want to reset?')"><b>Reset counter</b></a>]</td>
		  </tr>
		  <tr>
			 <td class="row1td" ><b>Visitors per day</b></td>
			 <td class="row2td" ><b><?php echo $avg_visitor?></b></td>
		  </tr>
		</table>
		<br />
		-->

		</td>
		  </tr>
				<tr>
				  <td bgcolor="#FFFFFF" >
		<table width="200"  border="0" cellspacing="5" cellpadding="0" align="center" >
		  <tr>
			 <td class="row1td">
				<table width="100%"  border="0" cellspacing="0" cellpadding="0" >
				<form name="select_year" id="select_year" >
						<tr align="center">
						  <td width="40%"><input type="button" class="btn_next_previous" onClick="load_chart_month(parseInt(document.select_year.current_year.value)-1)" value="  &laquo;  " title="Previous year"></td>
						  <td width="20%"><input type="text" class="current_year"  name="current_year" value="<?php echo $_GET['year']?>" readonly="true"  /></td>
						  <td width="40%"><input type="button" class="btn_next_previous"  onClick="load_chart_month(parseInt(document.select_year.current_year.value)+1)" value="  &raquo;  " title="Next year"></td>
						</tr>
				</form>
				</table>		
				</td>
		  </tr>
		  <!--
		  <tr><td align="center" style="font-size:14px ">( <b><?php echo intval($year_visitors)?></b> <?php echo $lang['visitors']?> )</td>
		  </tr>
		  -->
		</table></td>
		  </tr>
				<tr>
				  <td align="center" bgcolor="#FFFFFF" >
				  <div id="chart_visit_month">
				  <iframe name="frm_vpm" id="frm_vpm" src="jcas_modules/chart/visit_per_month.php?year=<?php echo $_GET['year']?>" width="600" height="300" frameborder="0" scrolling="no"></iframe>
				  </div>
				  </td>
		  </tr>
				
				<tr>
				  <td bgcolor="#FFFFFF" >
		<table width="200"  border="0" cellspacing="7" cellpadding="0" align="center" >
		  <tr>
			 <td align="center" class="row1td">
			  <?php echo $lang['view_visitors_in']?>
			 <select name="month" onChange="load_chart_day(this.options[this.selectedIndex].value);">
			 <?php
			 for($x=1;$x<=12;$x++)
			 {
				?><option value="<?php echo $x?>" <?php echo (($_GET['month']==$x || $current_month==$x)?'selected':'')?> style="font-weight:bold; color:blue"><?php echo $lang_month[$x]?></option><?php
			 }
			 ?>
			 </select>			 
			 </td>
		  </tr>
		  <!--
		  <tr><td align="center" style="font-size:11px ">( <b><?php echo intval($visitor_month[$_GET['month']])?></b> <?php echo $lang['visitors']?> )</td>
		  </tr>
		  -->
		</table></td>
		  </tr>
				<tr>
				  <td bgcolor="#FFFFFF" align="center" >
				  <div id="chart_visit_day"><iframe name="frm_vpd" id="frm_vpd" src="jcas_modules/chart/visit_per_day.php?year=<?php echo $_GET['year']?>&month=<?php echo $current_month?>" width="600" height="300" frameborder="0" scrolling="no"></iframe></div>
				  </td>
		  </tr>
		</table>
		</td>
		  </tr>
		</table>
<?php
	break;
	
	case "page_hit":
		$q_page = $DB->query('SELECT * FROM '.$tbl['page_hit'].' ORDER BY hits DESC');
		$total_page = $DB->num_rows($q_page);
		
		$q_total = $DB->fetch_array($DB->query('SELECT SUM(hits) AS total FROM '.$tbl['page_hit'].''));
		$total_hits = $q_total['total'];
		
		?>
		<table width="100%"  border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
		
		  <tr>
			<td colspan="10" class="headtd"><?php echo $lang["page_hit_title"]?></td>
		  </tr>
		  <tr align="center">
			 	<td width="5%" class="subheadtd"><?php echo $lang['order']?></td>
			 	<td width="25%" class="subheadtd"><?php echo $lang['title']?></td>
				<td width="25%" class="subheadtd"><?php echo $lang['url']?></td>
				<td width="30%" class="subheadtd"><?php echo $lang['graph']?></td>
				<td width="10%" class="subheadtd"><?php echo $lang['hits']?></td>
				<td width="5%" class="subheadtd">&nbsp;</td>
		  </tr>
		  <?php
		  if($total_page)
		  {
				  $no=0;
				  for($x=0;$x<$total_page;$x++)
				  {
					$no+=1;
					$hit = $DB->result_array($q_page,$x,'id,title,page_code,hits');
					$percent = round($hit['hits']/$total_hits,2)*100;
					
					$url = $cfg['site_url'].'/?'.$hit['page_code'];
				  ?>
				  <tr onMouseOver="rollon(this);" onMouseOut="rolloff(this);" height="22">
					<td align="right" class="datatd" ><?php echo $no?></td>
					<td class="datatd" align="left"><strong><?php echo data($hit['title'])?></strong></td>
					<td class="datatd" align="left"><a href="<?php echo $url?>" target="_blank"><?php echo $url?></a></td>
					<td class="datatd" align="left">
					 <div style="width:<?php echo $percent?>%; height:15px; background-color:#990000; color:#FFFFFF"><?php echo (($percent>10)?$percent.'%':'')?></div>
					</td>
					<td class="datatd" align="right"><strong><?php echo number_format($hit['hits'])?></strong></td>
					<td class="datatd" align="center"><a href="<?php echo $index_url?>&p=page_hit&act=delete&id=<?php echo $hit['id']?>" onClick="return confirm('<?php echo $lang['js_confirm_delete']?>');" title="<?php echo $lang['delete_item']?>"><img src="images/icon/icon_delete.gif" width="11" height="13" border="0" /></a></td>
				  </tr>
				<?php
				}
			}
			else
			{
			?>
				<tr>
					 <td colspan="20" align="center" class="datatd"><?php echo message($lang['no_requested_data'],'info')?></td>
				  </tr>
			<?php
			}
		  ?>
		  <tr>
			 <td colspan="10" align="center" class="row1td" >
			 <?php echo js_validate("title,page_code","frm_add_stat");?>
				<table  border="0" cellpadding="1" cellspacing="0">
				<form name="frm_add_stat" method="post" action="<?php echo $index_url?>&p=page_hit" onsubmit="return validate_frm_add_stat()">
				  <tr >
					 <td align="center" ><strong><?php echo $lang['page_title_add']?></strong>:
				     <input name="title" type="text" id="title" size="25" />				     
					 <strong><?php echo $lang['page_url_add']?></strong>: ?
                     <input name="page_code" type="text" id="page" size="20" />	
					 </td>
					 <td>		     
					 <input type="submit" name="insert" value="<?php echo $lang['btn_insert']?>" class="button" />
					 </td>
				  </tr>
				  </form>
				</table>
			</td>
		  </tr>
		 
	</table>
	<?php
	break;
}
$_GET["p"] = false;
?>
